﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.YHP_TransManage.Test
{
    public partial class 泥人网络继电器 : Form
    {
        HardWareBLL.NRNetRelayBLL nRNetRelayBLL;

        //泥人网络继电器 i202
        public 泥人网络继电器()
        {
            InitializeComponent();
        }
        Socket client;
        private void button1_Click(object sender, EventArgs e)
        {
#if false
            ///创建客户端
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            ///IP地址
            IPAddress ip = IPAddress.Parse(textBoxIp.Text.Trim());
            ///端口号
            IPEndPoint endPoint = new IPEndPoint(ip, int.Parse(textBoxPort.Text.Trim()));
            ///建立与服务器的远程连接
            client.Connect(endPoint);
            ///线程问题
            //Thread thread = new Thread(ReciveMsg);
            //thread.IsBackground = true;
            //thread.Start(client);
            //将字符串指令转换为byte数组
            byte[] buf = System.Text.Encoding.Default.GetBytes("AT+STACH1=1,1\r\n");
            //发送AT指令
            client.Send(buf);

            byte[] recbuf = new byte[10];//真正使用时，接收缓冲区需要适当的调整
            //等待硬件响应命令，接收到的数据为byte数组
            //会等待到有数据返回为止
            int recLen = client.Receive(recbuf);
            client.Close();
            //将byte数组转为字符串
            string str = System.Text.Encoding.Default.GetString(recbuf);
            str = str.Replace("\r\n", "");
            MessageBox.Show(str); 
#endif
            byte num = Convert.ToByte(textBoxOut.Text.Trim());
            Common.ShowBlackBox.WriteLine("1 num is " + num);
            bool res = nRNetRelayBLL.HandleStach(num);
            if (!res)
            {
                Common.ShowBlackBox.WriteLine("3 send message fail");
                return;
            }
            Common.ShowBlackBox.WriteLine("1 send message success");
        }

      
        //init
        private void button3_Click(object sender, EventArgs e)
        {

            string a = "+OCCH1:0+OCCH2:1+OCCH3:0+OCCH4:0";
            string[] s = a.Replace("OCCH", "").Split('+');
            string aaa = string.Empty;
            foreach (string item in s)
            {
                if (!string.IsNullOrEmpty(item) && item.Split(':')[1] == "1")
                {
                    aaa =  item.Split(':')[0];
                    break;
                }
            }
            Models.TerminalM terminalM = new Models.TerminalM();
            terminalM.ip = textBoxIp.Text.Trim();
            terminalM.port = Convert.ToInt32(textBoxPort.Text.Trim());
            nRNetRelayBLL = new HardWareBLL.NRNetRelayBLL(terminalM);
            Common.ShowBlackBox.WriteLine("1 init success");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                num = Convert.ToInt32(textBox1.Text.Trim());
            }
            Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL SearchStateIn "+ nRNetRelayBLL.SearchStateIn(num));
            Common.ShowBlackBox.WriteLine("1 nRNetRelayBLL success");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int num = 0;
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                num = Convert.ToInt32(textBox1.Text.Trim());
            }
            textBox2.Text = nRNetRelayBLL.getIn(num).ToString();
        }
    }
}
